package com.northwind.config;

import com.northwind.realm.MyRealm;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.RememberMeManager;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class ShiroConfig {

    //将shiro过滤器工厂对象交给spring工厂进行管理
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        //创建shiro过滤器工厂
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //讲安全管理器对象交给shiro过滤器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        //创建map，设置多个过滤器
        HashMap<String , String> map = new HashMap<>();
        //设置过滤器   要过滤的资源路径   使用的过滤器(过滤规则)
        /*
         * anon 匿名过滤器
         * authc 认证过滤器
         *
         * */

        map.put("/**","authc");

        //配置匿名就可以访问的资源
        map.put("/bootstrap/**","anon");
        map.put("/login/assets/**","anon");
        map.put("/Echarts-js/**","anon");
        map.put("/goEasy-js/**","anon");
        map.put("/admin/getImageCode","anon");
        map.put("/admin/login","anon");
        map.put("/main/main.jsp","anon");




        //调用subject.login（）算认证通过，记住我也算认证通过
        map.put("/**","user");
        
        //配置退出过滤器
        map.put("/logout","logout");

        //权限过滤器
        //map.put("admin/add","perms[boss:add]");
        //设置登录页面的位置
        shiroFilterFactoryBean.setLoginUrl("/login/login.jsp");
        //设置shiro的过滤器链
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        //设置没权限跳转的提示页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/error/error.jsp");
        return shiroFilterFactoryBean;
    }

    //将安全管理器对象交给spring工厂进行管理
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(MyRealm myRealm,DefaultSessionManager sessionManager,CookieRememberMeManager cookieRememberMeManager,EhCacheManager cacheManager){
        //创建web安全管理器对象
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //将自定义的Realm交给安全管理器
        defaultWebSecurityManager.setRealm(myRealm);
        //将Session管理器交给安全管理器
        defaultWebSecurityManager.setSessionManager(sessionManager);
        //将记住我管理器交给安全管理器
        defaultWebSecurityManager.setRememberMeManager(cookieRememberMeManager);
        //将缓存管理对象交给安全管理器
        defaultWebSecurityManager.setCacheManager(cacheManager);
        return defaultWebSecurityManager;
    }

    //将自定义Realm对象交给spring工厂进行管理
    @Bean
    public MyRealm getmyRealm(HashedCredentialsMatcher credentialsMatcher,CacheManager cacheManager) {
        MyRealm myRealm = new MyRealm();

        //将凭证匹配器对象交给myRealm
        myRealm.setCredentialsMatcher(credentialsMatcher);



        return myRealm;
    }

    //将凭证匹配器对象交给spring工厂进行管理
    @Bean
    public HashedCredentialsMatcher getCredentialsMatcher(){
        //创建凭证匹配器对象
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("MD5");
        credentialsMatcher.setHashIterations(1024);
        return credentialsMatcher;
    }

    //将Session管理器对象交给spring工厂进行管理
    @Bean
    public DefaultSessionManager getDefaultSessionManager(){
        //创建Session管理
        DefaultSessionManager sessionManager = new DefaultSessionManager();
        //设置Session超时时间  单位:毫秒
        sessionManager.setGlobalSessionTimeout(1000*60*30);

        return sessionManager;
    }

    //将记住我管理器对象交给spring工厂进行管理
    @Bean
    public CookieRememberMeManager getcookieRememberMeManager(SimpleCookie cookie){
        //创建记住我管理器
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        //将cookie对象交给记住我管理器
        cookieRememberMeManager.setCookie(cookie);
        return cookieRememberMeManager;
    }

    //将cookie对象交给spring工厂进行管理
    @Bean
    public SimpleCookie getsimpleCookie(){
        //创建cookie对象
        SimpleCookie cookie = new SimpleCookie();
        cookie.setName("rememberme");
        cookie.setMaxAge(60*60*24*7);
        return cookie;
    }
    //将缓存交给Spring工厂
    @Bean
    public EhCacheManager getCacheManager(){
        //1.配置缓存
        EhCacheManager cacheManager = new EhCacheManager();
        return cacheManager;
    }
}
